package org.nbict.iot.task.realtime.func;

import com.alibaba.fastjson.JSONObject;
import org.apache.storm.trident.operation.BaseFunction;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * Created by songseven on 18/6/25.
 */
public class PacketJsonProject extends BaseFunction {


    private Fields fields;
    public PacketJsonProject(Fields fields) {
        this.fields = fields;
    }

    @Override
    public void execute(TridentTuple tuple, TridentCollector collector) {
        String platform = tuple.getString(0);
        String packet = tuple.getString(1); //packet
        Map<String, Object> map = (Map<String, Object>)JSONObject.parse(packet);

        Values values = new Values(); //values.add(platform);
        fields.forEach(f->{values.add(map.get(f));});
        collector.emit(values);
    }
}
